//
//  BasicNavigationController.m
//  XLDoctor
//
//  Created by longma on 2018/7/27.
//  Copyright © 2018年 XLH. All rights reserved.
//

#import "BasicNavigationController.h"

@interface BasicNavigationController ()

@end

@implementation BasicNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavigationBarTheme];
}

/// 重写push方法
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if (self.viewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:animated];
}

/// 设置UINavigationBar的主题
- (void)setupNavigationBarTheme{
    
    //适配xcode11,iOS13 全屏
    self.modalPresentationStyle = UIModalPresentationFullScreen;
    //第三方 //若要修改导航栏相关属性须加这句代码
    self.useSystemBackBarButtonItem = YES;
    //全局统一导航栏的样色（单例）
    [self setNavigationBarUI:[UINavigationBar appearance]];
}

- (void)setNavigationBarUI:(UINavigationBar *) appearance{
    //navigation标题文字颜色
    NSDictionary *titleTextAttributes = @{NSForegroundColorAttributeName : UIColor.mj_blackColor,NSFontAttributeName:MJUIFontBoldMake(18)};
    
        if (@available(iOS 15.0, *)) {
            
               UINavigationBarAppearance *barApp = MJHelper.shared.navBarApp;

                [barApp configureWithOpaqueBackground]; // 重置背景和阴影颜色
                barApp.backgroundColor = UIColor.mj_whiteColor; //背景颜色
            
                //基于backgroundColor或backgroundImage的磨砂效果
                barApp.backgroundEffect = nil;
               //阴影颜色（底部分割线），当shadowImage为nil时，直接使用此颜色为阴影色。如果此属性为nil或clearColor（需要显式设置），则不显示阴影。
                barApp.shadowColor = [UIColor clearColor];//隐藏阴影颜色（底部分割线）
                //标题文字颜色
                barApp.titleTextAttributes = titleTextAttributes;
                appearance.standardAppearance = barApp;
                appearance.scrollEdgeAppearance = barApp;
            
               //设置返回文字颜色 navigationItem控件的颜色（包含返回的箭头颜色）
               [appearance setTintColor:[UIColor whiteColor]];

        }else{
            //单例设置系统所有导航栏样色
        //    UINavigationBar *appearance = [UINavigationBar appearance];
        //    UINavigationBar *appearance = self.navigationBar;
            // 设置导航栏背景图片,用图片背景（当使用Image修改时候，下方BarTintColor失效）
            [appearance setBackgroundImage:[UIImage mj_imageWithColor:UIColor.mj_backgroundColor] forBarMetrics:UIBarMetricsDefault];//（iOS15 上面无效果）使用 UINavigationBarAppearance
            
            [appearance setShadowImage:[[UIImage alloc] init]];//隐藏导航栏底部的线条
            
            //设置导航栏背景 颜色
            appearance.translucent = NO;//设置为没有毛玻璃效果,默认情况下为半透明效果
            //[appearance setBarTintColor:[UIColor whiteColor]];//修改NavigationBar背景颜色 （设置系统弹出相册上面导航栏的颜色***）（iOS15 上面没效果）上面设置了 setBackgroundImage 所以此无效
            
            // 设置中部文字属性
            [appearance setTitleTextAttributes:titleTextAttributes];
            
            //设置返回文字颜色 navigationItem控件的颜色 （包含返回的箭头颜色）
            [appearance setTintColor:[UIColor whiteColor]];
        }
}

#pragma mark - 横竖屏
- (BOOL)shouldAutorotate {
    return self.topViewController.shouldAutorotate;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return self.topViewController.supportedInterfaceOrientations;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return self.topViewController.preferredInterfaceOrientationForPresentation;
}

@end
